using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIVideoPlayer : UIEntityWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIVideoPlayer),
            layer = EUILayer.Middle,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UIVideoPlayer
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExRawImage videoPlayer { protected set; get; }
            public UnityEngine.CanvasGroup alpha { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExRawImage>("videoPlayer", out var __tbv0);
                this.videoPlayer = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.CanvasGroup>("alpha", out var __tbv1);
                this.alpha = __tbv1;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        private VideoPlayerForUI videoPlayer;

        protected UIB_UIVideoPlayer ui { get; set; }

        protected override void BeforeInit()
        {
            ui = new UIB_UIVideoPlayer();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {

        }

        protected override void OnShow(UIBaseData data)
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {

        }

        public ExRawImage panel => this.ui.videoPlayer;
        public CanvasGroup alphaCtrl => this.ui.alpha;

        public async UniTask Play(string videoPath, bool loop = false, float speed = 1, bool useAlpha = false)
        {
            this.videoPlayer = ManagerCenter.Video.GetVideoPlayerForUI(useAlpha);
            this.ui.videoPlayer.gameObject.SetActive(true);
            this.videoPlayer.SetVideoTarget(this.ui.videoPlayer);
            await videoPlayer.Play(videoPath, loop, speed);
        }
    }
}
